﻿using System;
using System.Web.Security;
using Ornament.MemberShip;
using Ornament.MemberShip.Permissions;
using Ornament.Messages;
using Ornament.Messages.Dao;
using Ornament.Web.Messages;
using Ornament.Web;
using Ornament.Web.DashBoard;

namespace Ornament.MVCWebFrame.Models
{
    public class BaseDataInit
    {
        public void Build()
        {
            CreateMemberShip();
            CreateInfoType();
            IniShotcut();
        }

        private static void CreateMemberShip()
        {
            var userResource = new GenericResourceInfo<string>("User", typeof(UserOperator))
            {
                Name = "用户数据"
            };

            var roleResource = new GenericResourceInfo<string>("Role", typeof(RoleOperator))
            {
                Name = "角色"
            };

            var memberResource = new GenericResourceInfo<string>("Member", typeof(UserOperator))
            {
                Name = "用户个人信息"
            };

            var resDao = OrnamentContext.Current.MemberShipFactory.CreateResourceDao();
            resDao.SaveOrUpdate(userResource);
            resDao.SaveOrUpdate(roleResource);
            resDao.SaveOrUpdate(memberResource);

            var userPermission = new Permission(userResource)
                                     {
                                         Name = "用户完全控制许可",
                                         Comment = "用户管理员,所有与用户有关的权限、分组、组织单元",
                                         Operator = Convert.ToInt32(
                                             UserOperator.Approve | UserOperator.Lock | UserOperator.Modify |
                                             UserOperator.Read | UserOperator.SetPassword)
                                     };


            var rolePermission = new Permission(roleResource)
                                     {
                                         Name = "角色完全控制许可",
                                         Comment = "角色完全控制,包括分配，删除、新增操作",
                                         Operator =
                                             Convert.ToInt32(RoleOperator.Assign | RoleOperator.Modify |
                                                             RoleOperator.Read)
                                     };


            var memberPermission = new Permission(memberResource)
                                       {
                                           Name = "账户管理许可证",
                                           Comment = "用户管理自己本身信息的许可证",
                                           Operator = Convert.ToInt32(UserOperator.SetPassword | UserOperator.Modify)
                                       };


            IPermissionDao permissionDao = OrnamentContext.Current.MemberShipFactory.CreatePermissionDao();
            permissionDao.SaveOrUpdate(rolePermission);
            permissionDao.SaveOrUpdate(userPermission);
            permissionDao.SaveOrUpdate(memberPermission);


            //创建角色
            var admin = new Role("admin") { Comment = "超级管理员" };
            admin.Permissions.Add(rolePermission);
            admin.Permissions.Add(userPermission);

            var member = new Role("member") { Comment = "系统成员" };
            member.Permissions.Add(memberPermission);

            IRoleDao roleDao = OrnamentContext.Current.MemberShipFactory.CreateRoleDao();
            roleDao.SaveOrUpdate(admin);
            roleDao.SaveOrUpdate(member);

            //创建管理员

            IUserDao userDao = OrnamentContext.Current.MemberShipFactory.CreateUserDao();
            MembershipCreateStatus status;
            MembershipUser u = Membership.CreateUser("admin", "123456", "admin@admin.com", "admin", "123456",
                                                     true, out status);

            User user = userDao.Get("admin");

            user.AddRole(member);
            user.AddRole(admin);

            userDao.SaveOrUpdate(user);
        }

        private static void CreateInfoType()
        {
            IInfoTypeDao dao = OrnamentContext.Current.MessageManager.Factory.InfoTypeDao;

            var systemType = new InfoType("System");
            dao.Save(systemType);

            var userType = new InfoType(MessageType.User.ToString());
            Template template = userType.CreateTemplate("work", "~/Messages/Templates/System/User/work.htm");
            userType.AddTemplate(template);
            dao.SaveOrUpdate(userType);

            var accountType = new InfoType(MessageType.Account.ToString());
            template = accountType.CreateTemplate("work", "~/Messages/Templates/common.htm");
            accountType.AddTemplate(template);
            dao.SaveOrUpdate(accountType);

            var applicationType = new InfoType(MessageType.Application.ToString());
            template = applicationType.CreateTemplate("work", "~/Messages/Templates/common.htm");
            applicationType.AddTemplate(template);
            dao.SaveOrUpdate(applicationType);

            var workListType = new InfoType(MessageType.WorkList.ToString());
            template = workListType.CreateTemplate("work", "~/Messages/Templates/common.htm");
            workListType.AddTemplate(template);
            dao.SaveOrUpdate(workListType);

            systemType.Add(applicationType);
            systemType.Add(accountType);
            systemType.Add(userType);
            dao.SaveOrUpdate(systemType);

            //example
            for (int i = 0; i < 30; i++)
            {
                User user = OrnamentContext.Current.MemberShipFactory.CreateUserDao().Get("admin");
                const string content = "默认管理帐户admin已经加入,请尽快修改默认<b><a href=\"/Account/Info\">密码</a></b>";
                Info message = OrnamentContext.Current.MessageManager.CreateInfo(workListType, content, "admin");
                message.AddReaders(user);
                OrnamentContext.Current.MessageManager.Save(message);
            }
        }

        private static void IniShotcut()
        {
            var dao = OrnamentContext.Current.WebFactory.ShortcutDao;
            var a = new Shortcut()
            {
                Url = "../../Content/images/DashBroadIcon/id_card.png",
                Text = "添加用户",
                Tooltip = "为系统添加新的用户"

            };
            dao.SaveOrUpdate(a);
        }
    }
}